package com.silverbox.shopper.dubboApi.info;

import java.util.*;

import com.silverbox.sweeporder.controller.index.vo.PromotionInfoVO;
import com.silverbox.sweeporder.controller.waimai.outvo.ShopBusinessInfoVo;
import com.silverbox.waimai.entity.TakeOutBilling;
import com.silverbox.waimai.entity.TakeOutConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.silverbox.core.annotation.DataSource;
import com.silverbox.core.exception.CoreException;
import com.silverbox.core.exception.CoreException.CoreExceptionCode;
import com.silverbox.core.util.StringUtils;
import com.silverbox.core.util.Utils;
import com.silverbox.shopper.entity.BusinessEncoding;
import com.silverbox.shopper.entity.BusinessWhiteList;
import com.silverbox.shopper.entity.DiningArea;
import com.silverbox.shopper.entity.EpayShopConfig;
import com.silverbox.shopper.entity.Machine;
import com.silverbox.shopper.entity.OuterSupport;
import com.silverbox.shopper.entity.PayChannel;
import com.silverbox.shopper.entity.Qrcode;
import com.silverbox.shopper.entity.ReserveBasicInfo;
import com.silverbox.shopper.entity.Shopper;
import com.silverbox.shopper.entity.ShopperConfig;
import com.silverbox.shopper.entity.ShopperExtend;
import com.silverbox.shopper.entity.ShopperNotice;
import com.silverbox.shopper.entity.ShopperService;
import com.silverbox.shopper.entity.ThirdPartyAuthorization;
import com.silverbox.shopper.entity.ThirdUserInfo;
import com.silverbox.shopper.entity.extend.DecorationShopConfigDetailExtend;
import com.silverbox.shopper.entity.extend.EmployeeExtend;
import com.silverbox.shopper.entity.extend.InvoiceServiceInfo;
import com.silverbox.shopper.entity.extend.ShopperActivities;
import com.silverbox.shopper.entity.extend.ShopperAndParent;
import com.silverbox.shopper.entity.extend.ShopperInfo;
import com.silverbox.shopper.service.info.ShopperInfoService;

@Service
@DataSource(name = DataSource.DATASOURCE_A)
public class ShopperInfoServiceApiImpl implements ShopperInfoServiceApi {

	@Autowired
	private ShopperInfoService shopperInfoService;
	
	@Override
	public Shopper getShopper(Integer storeId) {
		if (storeId == null) {
			throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.getShopper(storeId);
	}
	
	@Override
	public ShopperAndParent getShopperAndParent(Integer storeId) {
		if (storeId == null) {
			throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.getShopperAndParent(storeId);
	}

	@Override
	public DiningArea getDiningAreaById(Integer Id) {
		if (Id == null) {
			throw new CoreException("Id is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.getDiningAreaById(Id);
	}

	@Override
	public DiningArea getDiningAreaByUid(String uid) {
		if (Utils.isEmpty(uid)) {
			throw new CoreException("uid is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.getDiningAreaByUid(uid);
	}

	@Override
	public Map<String, Object> getShopperAllConfig(Integer storeId) {
		if (storeId == null) {
			throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		ShopperConfig shopperConfig = new ShopperConfig();
		shopperConfig.setShopperId(storeId);
		List<ShopperConfig> shopperConfigs = shopperInfoService.getShopperConfig(shopperConfig);
		Map<String, Object> map = new HashMap<String, Object>();
		if (!CollectionUtils.isEmpty(shopperConfigs)) {
			for (ShopperConfig sc : shopperConfigs) {
				// 如果已经存在配置，则跳过此次循环（店铺配置有重复脏数据时，使用后添加的）
				if (map.containsKey(sc.getPropName())) {
					continue;
				}
				if("CTR_ALIYUN_MQTT".equals(sc.getPropName())){
					sc.setPropValue("1");
				}
				if(!StringUtils.isEmpty(sc.getPropName())){
					map.put(sc.getPropName(), sc.getPropValue());
				}
			}
		}
		return map;
	}

	@Override
	public ShopperConfig getShopperConfig(Integer storeId, String propName) {
		if (storeId == null) {
			throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (propName == null) {
			throw new CoreException("propName is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		ShopperConfig shopperConfig = new ShopperConfig();
		shopperConfig.setShopperId(storeId);
		shopperConfig.setPropName(propName);
		List<ShopperConfig> shopperConfigs = shopperInfoService.getShopperConfig(shopperConfig);
		ShopperConfig shopper =null;
		if(!CollectionUtils.isEmpty(shopperConfigs)){
			shopper = new ShopperConfig();
			shopper = shopperConfigs.get(0);
		}
		return shopper;
	}

	@Override
	public ShopperNotice getShopperNotice(Integer storeId, String nowDate) {
		if (storeId == null) {
			throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (nowDate == null) {
			throw new CoreException("nowDate is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.getShopperNotice(storeId, nowDate);
	}

	@Override
	public ThirdPartyAuthorization getThirdPartyAuthorization(Integer storeId) {
		if (storeId == null) {
			throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.getThirdPartyAuthorization(storeId);
	}

	@Override
	public ThirdUserInfo getThirdUserInfo(String buyerId, Integer source) {
		if (buyerId == null) {
			throw new CoreException("buyerId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if(null == source){
			throw new CoreException("source is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.getThirdUserInfo(buyerId, source);
	}

	@Override
	public ShopperExtend getShopperExtend(Integer storeId) {
		if (storeId == null) {
			throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.getShopperExtend(storeId);
	}

	@Override
	public List<ShopperActivities> getShopperActivities(String shopId,String nowDate) {
		if (shopId == null) {
			throw new CoreException("shopId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (nowDate == null) {
			throw new CoreException("nowDate is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.getShopperActivities(shopId, nowDate);
	}

	@Override
	public List<ShopperService> getShopperService(Integer storeId) {
		if (storeId == null) {
			throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.getShopperService(storeId);
	}

	@Override
	public EpayShopConfig getEpayShopConfig(Integer storeId) {
		if (storeId == null) {
			throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.getEpayShopConfig(storeId);
	}

	@Override
	public EmployeeExtend getRelTableEmployee(Integer tableNo) {
		if (tableNo == null) {
			throw new CoreException("tableNo is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.getRelTableEmployee(tableNo);
	}

	@Override
	public List<Machine> getMachineByStoreId(Integer storeId) {
		if (storeId == null) {
			throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.getMachineByStoreId(storeId);
	}

	@Override
	public ShopperConfig getShopperConfigByShopperId(Integer shopperId, String propName) {
		if (shopperId == null) {
			throw new CoreException("shopperId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (propName == null) {
			throw new CoreException("propName is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		ShopperConfig shopperConfig = new ShopperConfig();
		shopperConfig.setShopperId(shopperId);
		shopperConfig.setPropName(propName);
		List<ShopperConfig> shopperConfigs = shopperInfoService.getShopperConfig(shopperConfig);
		return Utils.isEmpty(shopperConfigs)?null:shopperConfigs.get(0);
	}

	@Override
	public Map<String, Object> getShopperRenovationDefaultConfig(int decorationType) {
		return shopperInfoService.getShopperRenovationDefaultConfig(decorationType);
	}

	@Override
	public List<Long> getShopperRenovationByStoreId(Integer storeId,int decorationType) {
		if (storeId == null) {
			throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.getShopperRenovationByStoreId(storeId,decorationType);
	}

	@Override
	public String getShopperRenovationByShopperId(Long configId,int decorationType) {
		if (configId == null) {
			throw new CoreException("shopperId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.getShopperRenovationByShopperId(configId,decorationType);
	}

	@Override
	public Integer getWhiteBusinessStatus(String code) {
		if (Utils.isEmpty(code)) {
			throw new CoreException("code is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.getWhiteBusinessStatus(code);
	}

	@Override
	public boolean getIsOpenWhiteBusiness(Integer storeId, String code) {
		if (null == storeId) {
			throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (Utils.isEmpty(code)) {
			throw new CoreException("code is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.getIsOpenWhiteBusiness(storeId, code);
	}

	public ShopperInfo getShopperInfo(Integer storeId, String nowDate) {
		if (null == storeId) {
			throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (Utils.isEmpty(nowDate)) {
			throw new CoreException("nowDate is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.getShopperInfo(storeId,nowDate);
	}

	@Override
	public int saveOrUpdateThirdUserInfo(ThirdUserInfo thirdUserInfo) {
		if(StringUtils.isEmpty(thirdUserInfo.getThirdUserId())){
			throw new CoreException("thirdUserId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if(thirdUserInfo.getSource()==null){
			throw new CoreException("source is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if(StringUtils.isEmpty(thirdUserInfo.getUserId())){
			throw new CoreException("userId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		
		return shopperInfoService.saveOrUpdateThirdUserInfo(thirdUserInfo);
	}

	@Override
	public List<BusinessWhiteList> getShopperBusinessWhiteList(Integer storeId,Integer shopperId,String code) {
		if(storeId == null){
			throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if(shopperId == null){
			throw new CoreException("shopperId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.getShopperBusinessWhiteList(storeId,shopperId,code);
	}

	@Override
	public List<BusinessEncoding> getAllBusinessEncoding() {
		return shopperInfoService.getAllBusinessEncoding();
	}

	@Override
	public Integer getShopperRenovationPage(String pageCode,String componentCode,int decorationType) {
		if(StringUtils.isEmpty(pageCode)){
			throw new CoreException("pageCode is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if(StringUtils.isEmpty(componentCode)){
			throw new CoreException("componentCode is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.getShopperRenovationPage(pageCode,componentCode,decorationType);
	}

	@Override
	public Integer getShopperRenovationCustomPage(Integer storeId, String pageCode, String componentCode,int decorationType) {
		if(storeId==null){
			throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if(StringUtils.isEmpty(pageCode)){
			throw new CoreException("pageCode is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if(StringUtils.isEmpty(componentCode)){
			throw new CoreException("componentCode is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.getShopperRenovationCustomPage(storeId,pageCode,componentCode,decorationType);
	}

	@Override
	public List<DecorationShopConfigDetailExtend> getShopperPageDetail(Integer storeId,int decorationType) {
		if(storeId==null){
			throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.getShopperPageDetail(storeId,decorationType);
	}

	@Override
	public List<DecorationShopConfigDetailExtend> getShopperDefaultPageDetail(int decorationType) {
		return shopperInfoService.getShopperDefaultPageDetail(decorationType);
	}

	@Override
	public boolean getShopperRenovationStyleByStoreId(Integer storeId,int decorationType) {
		if(storeId==null){
			throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.getShopperRenovationStyleByStoreId(storeId,decorationType);
	}

	@Override
	public boolean getMachineConfig(Integer storeId) {
		if(storeId==null){
			throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.getMachineConfig(storeId);
	}

	@Override
	public boolean isSaoBei(Integer storeId, Integer shopperId) {
		if(storeId==null && shopperId==null){
			throw new CoreException("参数不能为null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.isSaoBei(storeId,shopperId);
	}
	
	@Override
	public OuterSupport getOuterSupportById(Long id) {
		if(id==null){
			throw new CoreException("参数不能为null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.getOuterSupportById(id);
	}

	@Override
	public List<PayChannel> getAntPayModel(Integer storeId) {
		return shopperInfoService.getAntPayModel(storeId);
	}

	@Override
	public ThirdUserInfo getThirdUserInfoById(Long id) {
		if(id==null){
			throw new CoreException("参数不能为null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.getThirdUserInfoById(id);
	}

	@Override
	public DiningArea getDiningAreaByTableName(String tableName,Integer storeId) {
		if(StringUtils.isEmpty(tableName)){
			throw new CoreException("参数不能为null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.getDiningAreaByTableName(tableName,storeId);
	}

	@Override
	public Qrcode getQrcodeInfoByGuid(String guid) {
		if(StringUtils.isEmpty(guid)){
			throw new CoreException("guid参数不能为null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.getQrcodeInfoByGuid(guid);
	}

	@Override
	public boolean isWxPay(Integer storeId, Integer shopperId) {
		if(storeId==null && shopperId==null){
			throw new CoreException("参数不能为null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.isWxPay(storeId,shopperId);
	}

	@Override
	public int updateIndex(Integer storeId) {
		if(storeId==null){
			throw new CoreException("店铺ID不能为null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.updateIndex(storeId);
	}

	@Override
	public int updateIndexs(Integer storeId, Integer orderType, Integer index) {
		if(storeId==null){
			throw new CoreException("店铺ID不能为null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if(orderType==null){
			throw new CoreException("订单类型不能为null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.updateIndexs(storeId, orderType, index);
	}

	@Override
	public Shopper selectIndex(Integer storeId, Integer orderType) {
		if(storeId==null){
			throw new CoreException("店铺ID不能为null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if(orderType==null){
			throw new CoreException("订单类型不能为null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.selectIndex(storeId, orderType);
	}

	@Override
	public List<PayChannel> findShopPayChannel(Integer payType, Integer storeId, Integer shopperId) {
		if(payType==null){
			throw new CoreException("支付类型不能为null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.findShopPayChannel(payType, storeId,shopperId);
	}

	@Override
	public List<InvoiceServiceInfo> findInvoiceServiceInfo(Integer storeId) {
		if(storeId==null){
			throw new CoreException("storeId不能为null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.findInvoiceServiceInfo(storeId);
	}

    @Override
    public Integer findInvoiceType(Integer storeId) {
        if(storeId==null){
            throw new CoreException("storeId不能为null", CoreExceptionCode.EXCP_PARAM_NULL);
        }
        return shopperInfoService.findInvoiceType(storeId);
    }
	@Override
	public List<ReserveBasicInfo> getReserveBasicInfo(Integer shopId, Integer storeId) {
		if(storeId==null){
			throw new CoreException("storeId不能为null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if(shopId==null){
			throw new CoreException("shopperId不能为null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.getReserveBasicInfo(shopId,storeId);
	}

	@Override
	public List<String> getReserveStageListByStoreId(Integer shopId, Integer storeId) {
		if(storeId==null){
			throw new CoreException("storeId不能为null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if(shopId==null){
			throw new CoreException("shopId不能为null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.getReserveStageListByStoreId(shopId,storeId);
	}

	@Override
	public ReserveBasicInfo getReserveBasicInfoByTime(Integer shopId, Integer storeId, String reserveTime) {
		if(storeId==null){
			throw new CoreException("storeId不能为null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if(shopId==null){
			throw new CoreException("shopId不能为null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.getReserveBasicInfoByTime(shopId,storeId,reserveTime);
	}

	@Override
	public List<ShopperNotice> getNoticeDetail(Integer storeId, String nowDate) {
		if(storeId==null){
			throw new CoreException("storeId不能为null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if(nowDate==null){
			throw new CoreException("nowDate不能为null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperInfoService.getNoticeDetail(storeId,nowDate);
	}

    @Override
    public String getWaimaiBusinessInfo(Integer storeId, Integer takeOutPeriodType) {
        return shopperInfoService.getWaimaiBusinessInfo(storeId,takeOutPeriodType);
    }

    @Override
    public ShopBusinessInfoVo getShopBusinessInfo(Integer storeId) {
        return shopperInfoService.getShopBusinessInfo(storeId);
    }

    @Override
    public List<Map<String, Object>> getStoreLogoListByStoreIds(List<Long> storeIds) {
        return shopperInfoService.getStoreLogoListByStoreIds(storeIds);
    }

    @Override
    public Long getShopIdByAppId(String appId) {
        return shopperInfoService.getShopIdByAppId(appId);
    }

    @Override
    public List<PromotionInfoVO> findStorePromotionInfoList(Integer storeId) {
        return shopperInfoService.findStorePromotionInfoList(storeId);
    }

    @Override
    public List<Long> getShopIdListByAppId(String appId) {
        return shopperInfoService.getShopIdListByAppId(appId);
    }

    @Override
    public List<String> getThirdUserInfoByMemberUid(String memberUid) {
        return shopperInfoService.getThirdUserInfoByMemberUid(memberUid);
    }

    @Override
    public List<String> getMiniConfigChannelAndMiniTypeKey(String appId) {
        List<String> keys = new LinkedList<>();
        List<Map<String, Object>> miniConfigs = shopperInfoService.getMiniConfigChannelAndMiniTypeKey(appId);
        for (Map<String, Object> keyMap : miniConfigs) {
            StringBuilder keyBuilder = new StringBuilder();
            String channel = (String) keyMap.get("channel");
            String miniType = (String) keyMap.get("miniType");
            if(Utils.isEmpty(channel) || Utils.isEmpty(miniType)){
                continue;
            }
            keyBuilder.append("IS_OPEN_").append(channel.toUpperCase()).append("_MINI_").append(miniType.toUpperCase());
            keys.add(keyBuilder.toString());
        }
        return keys;
	}

    @Override
    public List<Long> getShopIdListByConfigKey(List<String> configKeys, String appId) {
        return shopperInfoService.getShopIdListByConfigKey(configKeys,appId);
    }

    @Override
    public double findShopperBagPrice(Integer storeId) {
        return shopperInfoService.findShopperBagPrice(storeId);
    }

    @Override
    public Object findShopperConfigByShopperId(Integer storeId, String ctr_vip_chain_store) {
        return shopperInfoService.findShopperConfigByShopperId(storeId,ctr_vip_chain_store);
    }

    @Override
    public Map<String, Object> getShopLatAndLng(Integer storeId, Integer takeOutMethod) {
        return shopperInfoService.getShopLatAndLng(storeId,takeOutMethod);
    }

    @Override
    public TakeOutBilling findTakeOutBillingList(Integer storeId, Integer configId) {
        return shopperInfoService.findTakeOutBillingList(storeId,configId);
    }

    @Override
    public Map<String, Object> getBusinessTime(Integer storeId) {
        return shopperInfoService.getBusinessTime(storeId);
    }

    @Override
    public List<Map<String, Object>> findShopPeriod(Integer storeId) {
        return shopperInfoService.findShopPeriod(storeId);
    }

    @Override
    public Integer updateOrderSeq(Integer storeId, int indexType) {
        return shopperInfoService.updateOrderSeq(storeId,indexType);
    }

    @Override
    public String findAlipayStoreIdByShopper(Integer storeId) {
        return shopperInfoService.findAlipayStoreIdByShopper(storeId);
    }

    @Override
    public TakeOutConfig getTakeOutConfigByStoreId(Integer storeId) {
        return shopperInfoService.getTakeOutConfigByStoreId(storeId);
    }

}
